<template>
  <div class="resource-list">
    <el-card shadow="hover">
      <el-button @click="addAd">添加广告</el-button>
    </el-card>
    <el-card shadow="hover">
      <el-table :data="advertList"
                border
                style="width: 100%; margin-bottom: 20px"
                v-loading="isLoading">
        <el-table-column prop="id"
                         label="ID"
                         align="center"
                         width="100">
        </el-table-column>
        <el-table-column prop="name"
                         label="广告名称"
                         align="center"
                         width="180">
        </el-table-column>
        <el-table-column align="center"
                         label="广告位置">
          <template slot-scope="scope">
            <div>{{getLocation(scope.row.spaceId)}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center"
                         width="120"
                         label="广告图片">
          <template slot-scope="scope">
            <el-image :src="scope.row.img"
                      fit="contain" />
          </template>
        </el-table-column>
        <el-table-column width="250"
                         align="center"
                         label="时间">
          <template slot-scope="scope">
            开始时间：{{scope.row.startTimeFormatString}}<br>
            到期时间：{{scope.row.endTimeFormatString}}
          </template>
        </el-table-column>
        <el-table-column width="180"
                         align="center"
                         label="上线/下线">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status"
                       active-color="#13ce66"
                       inactive-color="#ff4949"
                       :active-value="1"
                       :inactive-value="0"
                       :disabled="scope.row.isStatusLoading"
                       @change="onStateChange(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column width="180"
                         align="center"
                         label="操作">
          <template slot-scope="scope">
            <el-button size="mini"
                       @click="handleEdit(scope.row.id)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAdList, updateStatus } from '@/services/advert'
import { getAdAllList } from '@/services/advert-space'

export default Vue.extend({
  name: 'AdvertList',
  data() {
    return {
      totalCount: 0,
      advertList: [
        {
          spaceId: '',
          adLocation: ''
        }
      ], // 资源分类列表
      isLoading: true, // 加载状态
      isEdit: true,
      advert: null,
      advertAllList: [
        {
          id: '',
          name: ''
        }
      ]
    }
  },
  created() {
    this.loadAdList()
  },

  methods: {
    async loadAdList() {
      const { data } = await getAdList()
      this.advertList = data.content
      this.loadAdAllList()
    },
    async loadAdAllList() {
      const { data } = await getAdAllList()
      this.advertAllList = data.content
      this.isLoading = false // 关闭加载中状态
    },
    test(index: any) {
      console.log(index)
    },
    addAd() {
      this.$router.push({
        name: 'advert-create'
      })
    },
    handleEdit(id: any) {
      this.$router.push({
        name: 'advert-edit',
        params: {
          id
        }
      })
    },
    getLocation(spaceId: any) {
      for (const item of this.advertAllList) {
        if (item.id === spaceId) {
          return item.name
        }
      }
    },
    async onStateChange(space: any) {
      await updateStatus(space)
      this.loadAdList()
    }
  }
})
</script>

<style lang="scss" scoped></style>
